﻿using System.IO;
using System.IO.IsolatedStorage;
using DDU.Infrastructure;

namespace DDU.DataModel
{
    public class StoredBookCoverInfo : NotificationObject
    {
        public StoredBookCoverInfo() { }

        public StoredBookCoverInfo(string fname)
        {
            FileName = fname;
        }

        private string _fileName;
        public string FileName {
            get { return _fileName; }
            set
            {
                _fileName = value;
                RaisePropertyChanged(() => FileName);
                RaisePropertyChanged(() => Stream);
            }
        }

        public Stream Stream
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(FileName))
                {
                    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        string fname = FileName;
                        if (store.FileExists(fname))
                        {
                            return IsolatedStorageFile.GetUserStoreForApplication().OpenFile(fname, FileMode.Open, FileAccess.Read);
                        }
                    }
                }
                return null;
            }
        }
    }
}
